import os.path

from common.log import logger


def draw_graph(graph, filename: str = "graph"):
    try:
        filepath = os.path.join(os.path.dirname(__file__), '../data', f'{filename}.jpg')
        mermaid_code = graph.get_graph().draw_mermaid_png()
        with open(filepath, "wb") as f:
            f.write(mermaid_code)

    except Exception as e:
        logger.error(f"Save graph failed: {e}")
        logger.exception(e)